/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(function(){
    $('div#body').show();
    
    $(".warning_section").hide();
    
    $(".tableStyle").chromatable({
        scrolling: 'yes',
        width: '850px',
        height: '500px',
        border: 'solid 1px #0e4d7b',
        background: '#F9FDFD'
    });
    
    
    $("#success_area").hide();
    $("#success_area").fadeIn(600);
    setTimeout(function(){
        $("#success_area").fadeOut(600);
    }, 6000);
    
});

//Code put in window load function because of problems caused by the display of 
//the menu items as box elements with a variable width and the calculation of
//that width occuring too soon (weird)
$(window).load(function() {
    $('.menuList').each(function(){
        $(this).width($(this).parent('.menuItem').width());
    });
});


function addItemToList(fromList, toList, callback){
    var exists = false;
    var value = fromList.val();
    var text = fromList.children("option[value='"+value+"']").text();
    toList.children("option").each(function(){
        if ($(this).val() == value){
            exists = true;
            return false;
        }
    });
    if (!exists){
        if (callback && typeof(callback) === "function") {
            callback(value, text);
        }
        toList.append("<option value='"+value+"'>"+text+"</option>");  
    }
    
}

/*Return true in the callback if you want the element to be removed.*/
function removeItemFromList(list, callback){
    list.children("option").each(function(){
        if ($(this).val() == list.val()){
            var element = $(this);
            if (callback && typeof(callback) === "function") {
                callback(element)
            }
            
            element.remove();
            return false;
        }
    });
    return false;
}
function removeMeal(){
    $("#meal_list option").each(function(){
        if ($(this).val() == $("#meal_list").val()){
            var element = $(this);
            $.post(getBaseURL()+"meal_allocation/meal_allocation/deleteMeal/", 
            {
                meal: element.val(),
                mealtime: $("#mealtime_id").val()
            },
            function(data){
                if (data == "good"){
                    element.remove();   
                }
                else{
                    alert("An error occurred.");
                }
            });
            return false;
        }
    });
}

function listboxRemoveItem(button){
    $(button).parents('.listboxItem').remove();
}

function addItemToListbox(fromList, toListbox, callback){
    var exists = false;
    var value = fromList.val();
    var text = fromList.children("option[value='"+value+"']").text();
    toListbox.children(".listboxItem").each(function(){
        if ($(this).attr('value') == value){
            exists = true;
            return false;
        }
    });
    if (!exists){
        if (callback && typeof(callback) === "function") {
            callback(value, text);
        }
        
        toListbox.append(createListboxItem(value,text));  
    }  
}

function createListboxItem(id,label){
    var span = $("<span />").addClass("listboxItem").attr('value',id);
    var label = $("<label >").addClass("listboxItemLabel").text(label);
    var anchor = $("<a />").addClass("listboxItemClose").attr({
        onclick:'listboxRemoveItem(this)'
    });

    span.append(label).append(anchor);
    return span;
}

jQuery.fn.cleanWhitespace = function() {
    textNodes = this.contents().filter(
        function() {return (this.nodeType == 3 && !/\S/.test(this.nodeValue));})
        .remove();
    return this;
}

function addFancybox(anchor){
    var options = {
        maxWidth	: 315,
        maxHeight	: 400,
        fitToView	: true,
        width		: '70%',
        height		: '70%',
        autoSize	: false,
        closeClick	: false,
        openEffect	: 'none',
        closeEffect	: 'none',
        modal           : true
    };
    
    $(anchor).fancybox(options);
}



function highlightSections(sections){
    //Unhighlight everything
    $(".highlighted").removeClass("highlighted");
    
    for(var i in sections){
        $(i.toString()).addClass("highlighted");
        $(i.toString()+" .warning_section").html(sections[i]);
    }
    
    $(".form_section").not('.highlighted').find('.warning_section').slideUp(500);

    $('.highlighted .warning_section').slideDown(500, function(){
        $(".form_section.highlighted select").change(function(){
                $(this).unbind('change');
                $(this).parents('.form_section.highlighted').removeClass("highlighted");
                $(this).siblings(".warning_section").slideUp();
        });
        
        $(".form_section.highlighted input").keyup(function(){
            if ($(this).attr('type') == 'text' || $(this).attr('type') == 'password'){
                $(this).parents('.form_section.highlighted').removeClass("highlighted");
                $(this).siblings(".warning_section").slideUp();
            }
        });
    });
}

function toggleHighlight(section, text){
    if (text == null){
        $(section).removeClass("highlighted");
        $(section+" .warning_section").slideUp(500);
    }
    else{
        $(section).addClass("highlighted"); 
        $(section+" .warning_section").html(text);
        $(section+" .warning_section").slideDown(500);
    }
}

function changeMade(){
    window.onbeforeunload = function() {
        return 'Changes have not been saved.';
    };
}

function saveMade(){
    window.onbeforeunload = null;
}


function boolToInt(boolval){
    if (boolval){
        return 1;
    }
    else{
        return 0;
    }
}